package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.OrganizationService;
import com.yixun.qfbao.apidto.GetDeptTreeReqDto;
import com.yixun.qfbao.apidto.GetDeptTreeResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetDeptTreeReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @classname: GetChangeDeptTreeHandler.java
 * @author: Beau
 * @create: 2019/11/19 11:53
 * @version: 1.0.0
 **/
@Service("getChangeDeptTreeHandler")
public class GetChangeDeptTreeHandler extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetChangeDeptTreeHandler.class);

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private LoginUtils loginUtils;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetDeptTreeReqVo req = (GetDeptTreeReqVo)reqDto;
        GetDeptTreeReqDto reqDto1 = req.getParams();
        BaseResponseDTO<GetDeptTreeResDto> res = new BaseResponseDTO<GetDeptTreeResDto>();
        GetDeptTreeResDto resDto = new GetDeptTreeResDto();
        try {
            if(StringUtils.isNotBlank(reqDto1.getUserId())){
                PartnerInfo partner = loginUtils.getUser(reqDto1.getUserId());
                reqDto1.setMerchantId(partner.getHigherid());
            }
            resDto = organizationService.getChangeDeptTree(reqDto1);
        }catch(TServerException e){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg(e.getErrorMsg());
        } catch (Exception e) {
            logger.error("GetChangeDeptTreeHandler error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("系统异常");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }

}
